﻿using System.Collections.ObjectModel;
using System.Windows.Controls;

namespace zaklad_rtv
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        public enum Tabs
        {
            ServiceList, ClientList, ServiceForm, ClientForm, Settings
        }

        public static DatabaseHelper DBHelper { get; set; }

        public MainWindow()
        {
            InitializeComponent();
            DBHelper = new DatabaseHelper();
            InitializeTabs();
        }

        private void InitializeTabs()
        {
            var clientList = new ClientList
            {
                ObjectCollection = new Collection<Client>(DBHelper.GetAllClient()),
                MainWindow = this
            };
            clientList.DatabaseObjectChanged += HandleClientChanged;
            _clientListTab.Content = clientList;

            var serviceList = new ServiceList
            {
                ObjectCollection = new Collection<Service>(DBHelper.GetAllService()),
                MainWindow = this
            };
            _serviceListTab.Content = serviceList;

            var clientForm = new ClientForm
            {
                MainWindow = this
            };
            clientForm.DatabaseObjectChanged += HandleClientChanged;
            _clientFormTab.Content = clientForm;

            var serviceForm = new ServiceForm
            {
                ClientCollection = new Collection<Client>(DBHelper.GetAllClient()),
                MainWindow = this
            };
            serviceForm.DatabaseObjectChanged += HandleServiceChanged;
            _serviceFormTab.Content = serviceForm;

            var configForm = new ConfigForm
            {
                MainWindow = this
            };
            _settingsTab.Content = configForm;
        }

        private void HandleServiceChanged(object sender, DatabaseObjectChangedEventArgs e)
        {
            if (e.Action.Equals(EventAction.Update))
            {
                var serviceList = _serviceListTab.Content as ServiceList;
                if (serviceList != null)
                    serviceList.ServiceEdited(e.Object as Service);
            }
            else if (e.Action.Equals(EventAction.Insert))
            {
                var serviceList = _serviceListTab.Content as ServiceList;
                if (serviceList != null)
                    serviceList.ServiceAdded(e.Object as Service);
            }
        }

        private void HandleClientChanged(object sender, DatabaseObjectChangedEventArgs e)
        {
            if (e.Action.Equals(EventAction.Delete))
            {
                var serviceForm = _serviceFormTab.Content as ServiceForm;
                if (serviceForm != null)
                    serviceForm.ClientDeleted(e.Object as Client);
            }
            else if (e.Action.Equals(EventAction.Update))
            {
                var clientList = _clientListTab.Content as ClientList;
                if (clientList != null)
                    clientList.ClientEdited(e.Object as Client);

                var serviceForm = _serviceFormTab.Content as ServiceForm;
                if (serviceForm != null)
                    serviceForm.ClientEdited(e.Object as Client);
            }
            else if (e.Action.Equals(EventAction.Insert))
            {
                var clientList = _clientListTab.Content as ClientList;
                if (clientList != null)
                    clientList.ClientAdded(e.Object as Client);

                var serviceForm = _serviceFormTab.Content as ServiceForm;
                if (serviceForm != null)
                    serviceForm.ClientAdded(e.Object as Client);
            }
        }

        public void ChangeActiveTab(Tabs tab)
        {
            TabItem tabItem = null;
            switch (tab)
            {
                case Tabs.ClientList:
                    tabItem = _clientListTab;
                    break;
                case Tabs.ClientForm:
                    tabItem = _clientFormTab;
                    break;
                case Tabs.ServiceForm:
                    tabItem = _serviceFormTab;
                    break;
                case Tabs.ServiceList:
                    tabItem = _serviceListTab;
                    break;
                case Tabs.Settings:
                    tabItem = _settingsTab;
                    break;
            }
            if (tabItem != null) tabItem.IsSelected = true;
        }
    }
}
